// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.network.GameClient;
import zombie.network.PacketTypes;

public final class WorldObjectsSyncRequests {
    public final ArrayList<WorldObjectsSyncRequests.SyncData> requests = new ArrayList();
    public long timeout = 1000L;

    public void putRequest(IsoChunk ch) {
        WorldObjectsSyncRequests.SyncData syncData = new WorldObjectsSyncRequests.SyncData();
        syncData.x = ch.wx;
        syncData.y = ch.wy;
        syncData.hashCodeWorldObjects = ch.getHashCodeObjects();
        syncData.reqTime = 0L;
        syncData.reqCount = 0;
        synchronized (this.requests) {
            this.requests.add(syncData);
        }
    }

    public void sendRequests(UdpConnection connection) {
        if (this.requests.size() != 0) {
            ByteBufferWriter byteBufferWriter = connection.startPacket();
            PacketTypes.PacketType.SyncWorldObjectsReq.doPacket(byteBufferWriter);
            ByteBuffer byteBuffer = byteBufferWriter.bb;
            int int0 = byteBuffer.position();
            byteBufferWriter.putShort((short)0);
            int int1 = 0;
            synchronized (this.requests) {
                for (int int2 = 0; int2 < this.requests.size(); int2++) {
                    WorldObjectsSyncRequests.SyncData syncData = (WorldObjectsSyncRequests.SyncData)this.requests.get(int2);
                    if (syncData.reqCount > 2) {
                        this.requests.remove(int2);
                        int2--;
                    } else {
                        if (syncData.reqTime == 0L) {
                            syncData.reqTime = System.currentTimeMillis();
                            int1++;
                            byteBuffer.putInt(syncData.x);
                            byteBuffer.putInt(syncData.y);
                            byteBuffer.putLong(syncData.hashCodeWorldObjects);
                            syncData.reqCount++;
                        }

                        if (System.currentTimeMillis() - syncData.reqTime >= this.timeout) {
                            syncData.reqTime = System.currentTimeMillis();
                            int1++;
                            byteBuffer.putInt(syncData.x);
                            byteBuffer.putInt(syncData.y);
                            byteBuffer.putLong(syncData.hashCodeWorldObjects);
                            syncData.reqCount++;
                        }

                        if (int1 >= 50) {
                            break;
                        }
                    }
                }
            }

            if (int1 == 0) {
                GameClient.connection.cancelPacket();
            } else {
                int int3 = byteBuffer.position();
                byteBuffer.position(int0);
                byteBuffer.putShort((short)int1);
                byteBuffer.position(int3);
                PacketTypes.PacketType.SyncWorldObjectsReq.send(GameClient.connection);
            }
        }
    }

    public void receiveIsoSync(int x, int y) {
        synchronized (this.requests) {
            for (int _int = 0; _int < this.requests.size(); _int++) {
                WorldObjectsSyncRequests.SyncData syncData = (WorldObjectsSyncRequests.SyncData)this.requests.get(_int);
                if (syncData.x == x && syncData.y == y) {
                    this.requests.remove(_int);
                }
            }
        }
    }

    private class SyncData {
        int x;
        int y;
        long hashCodeWorldObjects;
        long reqTime;
        int reqCount;
    }
}
